﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;

namespace BusinessRule.System
{
    public class ThirdPartyCompaniesManager : BusinessLogic
    {
        /// <summary>
        /// 保存第三方公司
        /// </summary>
        /// <param name="tpc"></param>
        public bool Save(THIRDPARTY_COMPANIES com)
        {
            bool success = false;
            if (com != null)
            {
                try
                {
                    com.Save();
                    success = true;
                }
                catch (Exception error)
                {
                    throw new Exception(error.Message);
                }
            }
            return success;
        }


        public THIRDPARTY_COMPANIES Load(decimal ID)
        {
            return (THIRDPARTY_COMPANIES)Jumpton.Persistent.ObjectRow.Load(typeof(THIRDPARTY_COMPANIES), "ID=" + ID);
        }

        public THIRDPARTY_COMPANIES LoadByCode(string code)
        {
            return (THIRDPARTY_COMPANIES)ObjectRow.Load(typeof(THIRDPARTY_COMPANIES), "Code='" + code + "'");
        }

        public void Delect(decimal ID)
        {
            SqlData.ExecuteSql("delete from THIRDPARTY_COMPANIES where ID =" + ID);
        }

        public void UpdateTHIRDPARTY_COMPANIESById(THIRDPARTY_COMPANIES tpc)
        {
            //tpc = Load(tpc.ID);
            //ThirdPartyCompanies obj = new ThirdPartyCompanies();

            //obj.Save();
        }


        public DataTable GetThirdPartyCompanies(decimal domain_id, string strWhere)
        {
            string sql = string.Format("select * from THIRDPARTY_COMPANIES where  DOMAIN_ID={0}", domain_id);
            if (strWhere != "")
            {
                sql +=  strWhere;
            }
            
            return SqlData.OpenSql(sql).Tables[0];
        }


        public DataTable GetThirdPartyCompaniesByOrg(decimal domain_id, string org)
        {
            string sql = string.Format(@"select  distinct t.*  from THIRDPARTY_COMPANIES t
                                        inner join THIRD_ORG_MEMBER m
                                        on m.THIRD_COM_ID = t.ID
                                        inner join ORGANIZATION o 
                                        on o.ID = m.ORG_ID 
                                        and o.ID in ({0}) and o.DOMAIN_ID={1} ",org,domain_id);
          
            return SqlData.OpenSql(sql).Tables[0];
        }

    }
}
